package edu.ztr.controller;

import edu.ztr.pojo.Actor;
import edu.ztr.pojo.PageBean;
import edu.ztr.pojo.Result;
import edu.ztr.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;

//    @GetMapping("/search")
//    public Result list() {
//        log.info("查询全部搜索数据");
//
//        // 调用service查询数据
//        List<Ele> SearchList = searchService.list();
//
//        return Result.success(SearchList);
//    }

    //     分页查询
    @GetMapping("/search")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(defaultValue = "") String content,
                       @RequestParam(defaultValue = "") String beginStr, // 修改这里，直接接收字符串
                       @RequestParam(defaultValue = "") String endStr) {

        LocalDate begin = null;
        LocalDate end = null;
        if (!beginStr.isEmpty() && !endStr.isEmpty()) {
            begin = parseToLocalDate(beginStr.substring(0, 19)); // 添加转换方法
            end = parseToLocalDate(endStr.substring(0, 19)); // 同上
        }

        log.info("分页查询参数{},{},{},{},{}", page, pageSize, content, begin, end);
        PageBean pageBean = searchService.page(page, pageSize, content, begin, end);
        return Result.success(pageBean);
    }

    @GetMapping("/search/delete")
    public Result deleteSearch(@RequestParam(defaultValue = "0") Integer id){
        boolean flag = searchService.deleteSearch(id);
        return flag ? Result.success() : Result.error("删除失败");
    }
    @GetMapping("/edit")
    public Result edit(@RequestParam(defaultValue = "0") Integer id,
                       @RequestParam(defaultValue = "") String time,
                       @RequestParam(defaultValue = "") String content,
                       @RequestParam(defaultValue = "") String href,
                       @RequestParam(defaultValue = "0") Integer way
    ) {
        System.out.println(id+">>>>>>>>>>>>>>>>");
        LocalDate t;
        if (time.isEmpty()) {
            t = null;
        } else {
            if(time.charAt(10)==' '){
                time = time.substring(0,10)+"T"+time.substring(11,19);
                t = parseToLocalDate(time.substring(0, 19));
            }else{
                t = parseToLocalDate(time.substring(0, 19));
                t = t.plusDays(1);
            }


        }
        boolean flag = searchService.edit(id, t, content, href, way);
        return flag ? Result.success() : way != 0 ? Result.error("添加失败") : Result.error("修改失败");
    }

    @GetMapping("/movie/getactor")
    public Result getActor(@RequestParam String id) {
        List<Actor> res = searchService.getActor(id);
        return Result.success(res);
    }

    @GetMapping("/movie/delete")
    public Result deleteMovie(@RequestParam(defaultValue = "0") Integer id) {
        boolean flag = searchService.deleteMovie(id);
        return flag ? Result.success() : Result.error("删除失败");
    }

    @GetMapping("/movie")
    public Result getMovie(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam(defaultValue = "") String name
    ) {
        LocalDate begin = null;
        LocalDate end = null;


        log.info("分页查询参数{},{},{},{},{}", page, pageSize, name);
        PageBean pageBean = searchService.getMovie(page, pageSize, name);
        return Result.success(pageBean);
    }

    // 获取api接口
    @GetMapping("/movie/api")
    public Result getApi() {
        List<String> res = searchService.getApi();
        return Result.success(res);
    }

    // 新增方法，用于解析字符串到LocalDate
    private LocalDate parseToLocalDate(String dateTimeStr) {
        try {
            // 假设dateTimeStr是ISO 8601格式，先转换为LocalDateTime，再提取日期部分
            LocalDateTime ldt = LocalDateTime.parse(dateTimeStr);
            System.out.println(ldt.toLocalDate());
            return ldt.toLocalDate();
        } catch (DateTimeParseException e) {
            // 处理解析错误，比如返回默认日期或抛出自定义异常
            throw new IllegalArgumentException("Invalid date format", e);
        }
    }
}
